package com.tuozixuan.shell.command;

import java.util.List;

import javax.validation.constraints.Size;

import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;

@ShellComponent
public class CommandDemo {

	/**
	 * 通过@ShellOption指定参数名称
	 */
	@ShellMethod("Echo params")
	public void echo(int a, int b, @ShellOption("--third") int c) {
	    System.out.println(String.format("a=%d, b=%d, c=%d", a, b, c));
	}
	
	/**
	 * 通过@ShellOption指定多个参数名称
	 */
	@ShellMethod("Echo command help")
	public void myhelp(@ShellOption({"-C", "--command"}) String cmd) {
	    System.out.println(cmd);
	}
	
	@ShellMethod("Say hello")
	public void hello1(@ShellOption(defaultValue = "World") String name) {
	    System.out.println("hello, " + name + "!");
	}
	
	/**
	 * 为一个参数传递多个值 add-by-array 1 2 3
	 */
	@ShellMethod("Add by array")
	public void addByArray(@ShellOption(arity = 3) int[] numbers) {
	    int sum = 0;
	    for(int number : numbers) {
	        sum += number;
	    }
	    System.out.println(String.format("sum=%d", sum));
	}
	
	@ShellMethod("Add by list")
	public void addByList(@ShellOption(arity = 3) List<Integer> numbers) {
	    int s = 0;
	    for(int number : numbers) {
	        s += number;
	    }
	    System.out.println(String.format("s=%d", s));
	}
	
	@ShellMethod("Shutdown action")
	public void shutdown(boolean shutdown) {
	    System.out.println(String.format("shutdown=%s", shutdown));
	}
	
	@ShellMethod("Echo.")
	public void echoStr(String what) {
	    System.out.println(what);
	}
	
	@ShellMethod("Change password")
	public void changePwd(@Size(min = 6, max = 30) String pwd) {
	    System.out.println(pwd);
	}
}
